The following describes the basic use of procedures and functions in Office Basic.
ここでは、Office Basic でのプロシージャーおよび関数の基本的な使用法について説明します。
![]() | When you create a new module, Office Basic automatically inserts a SUB called "Main". This default name has nothing to do with the order or the starting point of a Office Basic project. You can also safely rename this SUB.
モジュールを新規作成すると、Office Basic は自動的に「Main」という名前のプロシージャーを作成します。このデフォルト名は Office Basic プロジェクトの開始位置や順序には関係ありません。このプロシージャー名は任意に変更できます。 |
Procedures (SUBS) and functions (FUNCTIONS) help you maintaining a structured overview by separating a program into logical pieces.
プログラムの構造は、プロシージャー (SUB) や関数 (FUNCTION) を使用することで、個々の機能別に整理することができます。
One benefit of procedures and functions is that, once you have developed a program code containing task components, you can use this code in another project.
また、手続きや関数を使用するメリットの 1 つに、こうした特定の機能を担ったプログラムコード群を他のプロジェクトで再利用できる点があります。
Variables can be passed to both procedures and functions. The SUB or FUNCTION must be declared to expect parameters:
プロシージャーや関数には、変数を渡すことができます。これらの変数は、SUB および FUNCTION ステートメント部で使用を宣言する必要があります。
SUB SubName(Parameter1 As Type, Parameter2 As Type,...)
SUB SubName(Parameter1 As Type, Parameter2 As Type,...)
Program code
Program code
END SUB
END SUB
The SUB is called using the following syntax:
こうした SUB の呼び出しは、下記の構文で行います。
SubName(Value1, Value2,...)
SubName(Value1, Value2,...)
The parameters passed to a SUB must fit to those specified in the SUB declaration.
SUB に渡す個々の変数は、SUB 宣言部の変数型と一致する必要があります。
The same process applies to FUNCTIONS. In addition, functions always return a function result. The result of a function is defined by assigning the return value to the function name:
これらの事項は FUNCTION についても同様ですが、この場合は関数の戻り値が返されます。これらの戻り値は、各関数の処理ルーチンが終了するまでに算出しておき、戻り値とする計算結果のパラメータを関数名に代入する形で確定します(例を参照)。
FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...) As Type
FUNCTION FunctionName(Parameter1 As Type, Parameter2 As Type,...)As Type
Program code
Program code
FunctionName=Result
FunctionName=Result
End Function
End Function
The FUNCTION is called using the following syntax:
こうした FUNCTION の呼び出しは、下記の構文で行います。
Variable=FunctionName(Parameter1, Parameter2,...)
Variable=FunctionName(Parameter1, Parameter2,...)
![]() | You can also use the fully qualified name to call a procedure or function: Library.Module.Macro() For example, to call the Autotext macro from the Gimmicks library, use the following command: Gimmicks.AutoText.Main() プロシージャまたは関数を呼び出すには、完全修飾名を使うこともできます: Library. Module. Macro() たとえば、Grimmicks ライブラリにある Autotext マクロを呼び出すには、以下のようにコマンドを使います: Gimmicks.AutoText.Main() |
Parameters can be passed to a SUB or a FUNCTION either by reference or by value. Unless otherwise specified, a parameter is always passed by reference. That means that a SUB or a FUNCTION gets the parameter and can read and modify its value.
SUB や FUNCTION に渡すパラメータは、参照渡しか値渡しのいずれかの形で与えることができます。明示的に指定しない限り、パラメータは参照渡しとして渡されます。この場合、SUB や FUNCTION に渡されたパラメータは、その値の読み取りだけでなく、値の変更が可能です。
If you want to pass a parameter by value insert the key word "ByVal" in front of the parameter when you call a SUB or FUNCTION, for example:
値渡しでパラメータを与えるには、下記の例のように SUB や FUNCTION の呼び出し時に「ByVal」キーワードをパラメータの直前に指定します。
Result = Function(ByVal Parameter)
Result = Function(ByVal Parameter)
In this case, the original content of the parameter will not be modified by the FUNCTION since it only gets the value and not the parameter itself.
この場合 FUNCTION 部には、パラメータ本体ではなくその値だけが渡されるため、パラメータ値が書き換えられることはありません。
A variable defined within a SUB or FUNCTION, only remains valid until the procedure is exited. This is known as a "local" variable. In many cases, you need a variable to be valid in all procedures, in every module of all libraries, or after a SUB or FUNCTION is exited.
SUB または FUNCTION 内に定義される変数であり、手続きが完了するまでの間だけ有効です。 これは「ローカル」変数です。 多くの場合、すべての手続き内、すべてのライブラリの各モジュール内、または SUB や FUNCTION が完了したあとで、変数を有効にしておく必要があります。
GLOBAL VarName As TYPENAME
GLOBAL VarName As TYPENAME
The variable is valid as long as the Office session lasts.
こうして宣言した変数は、Office セッションの終了時まで有効となります。
PUBLIC VarName As TYPENAME
PUBLIC VarName As TYPENAME
The variable is valid in all modules.
こうして宣言した変数は、すべてのモジュールで有効となります。
PRIVATE VarName As TYPENAME
PRIVATE VarName As TYPENAME
The variable is only valid in this module.
こうして宣言した変数は、該当モジュールの内部だけで有効となります。
DIM VarName As TYPENAME
DIM VarName As TYPENAME
The variable is only valid in this module.
こうして宣言した変数は、該当モジュールの内部だけで有効となります。
Enforce private variables to be private across modules by setting CompatibilityMode(true).
CompatibilityMode(true) を設定して、プライベート変数がモジュール間でプライベートになるように強制的に設定します。
REM ***** Module1 *****
REM ***** Module1 *****
Private myText As String
Private myText As String
Sub initMyText
Sub initMyText
myText = "Hello"
myText = "Hello"
print "in module1 : ", myText
print "in module1 :", myText
End Sub
End Sub
REM ***** Module2 *****
REM ***** Module2 *****
'Option Explicit
'Option Explicit
Sub demoBug
Sub demoBug
CompatibilityMode( true )
CompatibilityMode( true )
initMyText
initMyText
' Now returns empty string
' 空の文字列を返します
' (or rises error for Option Explicit)
' (あるいは Option Explicit の場合はエラーを報告します)
print "Now in module2 : ", myText
print "in module1 :", myText
End Sub
End Sub
STATIC VarName As TYPENAME
STATIC VarName As TYPENAME
The variable retains its value until the next time the FUNCTION or SUB is entered. The declaration must exist inside a SUB or a FUNCTION.
宣言した変数は、次に SUB や FUNCTION を実行するまで有効となります。 この場合の変数宣言は、SUB や FUNCTION の内部に記述する必要があります。
As with variables, include a type-declaration character after the function name, or the type indicated by "As" and the corresponding key word at the end of the parameter list to define the type of the function's return value, for example:
関数の戻り値のデータ型の設定は、変数の場合と同様の型宣言子を用いて行い、下記の例のように、関数宣言時の関数名とパラメータリストに続けて「As」および型宣言用キーワードを記述することで指定します。
Function WordCount(WordText as String) as Integer
Function WordCount(WordText as String) as Integer